layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //新闻列表
    var tableIns = table.render({
        elem: '#newsList',
        url: $("#baseURL").val() + 'news/list',
        cellMinWidth: 95,
        page: true,
        limit: 10,
        limits: [10, 15, 20, 25],
        id: "newsListTable",
        cols: [[{
            type: "checkbox",
            fixed: "left",
            width: 50
        }, {
            field: 'id',
            title: 'ID',
            width: 60,
            align: "center"
        }, {
            field: 'newsName',
            title: '文章标题',
            width: 350
        }, {
            field: 'newsAuthor',
            title: '发布者',
            align: 'center'
        }, {
            field: 'newsStatus',
            title: '发布状态',
            align: 'center',
            templet: "#newsStatus"
        }, {
            field: 'newsLook',
            title: '浏览权限',
            align: 'center',
            templet: function (d) {
                if (d.newsLook == 0) {
                    return "私密浏览";
                } else if (d.newsLook == 1) {
                    return "公开浏览";
                } else {
                    return "";
                }
            }
        }, {
            field: 'newsTop',
            title: '是否置顶',
            align: 'center',
            templet: "#topTpl"
        }, {
            field: 'newsTime',
            title: '发布时间',
            align: 'center',
            minWidth: 110,
            templet: function (d) {
                return getMyDateTime(d.newsTime);
            }
        }, {
            title: '操作',
            width: 170,
            templet: '#newsListBar',
            fixed: "right",
            align: "center"
        }]]
    });

    //是否置顶
    form.on('switch(newsTop)', function (data) {
        var index = layer.msg('修改中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.ajax({
            url: $("#baseURL").val() + 'news/update',
            data: {
                "id": data.value,
                "newsTop": data.elem.checked ? 1 : 0
            },
            dataType: 'json',
            cache: false,
            type: 'post',
            success: function (data) {
                layer.close(index);
                if (data.code == 1) {
                    if (data.data.newsTop == 1) {
                        layer.msg("置顶成功！");
                    } else {
                        layer.msg("取消置顶成功！");
                    }
                } else {
                    layer.msg('设置失败：' + data.msg);
                }
            },
            error: function () {
                layer.close(index);
                layer.msg('系统异常,请联系系统管理员');
            }
        });
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() != '') {
            table.reload("newsListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    "newsName": $(".searchVal").val()
                }
            })
        } else {
            layer.msg("请输入搜索的内容");
        }
    });

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            newsId = [];
        if (data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }

            layer.confirm('确定删除选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
                $.get($("#baseURL").val() + 'news/deleteBatch', {
                    ids: newsId
                }, function (data) {
                    if (data.code == 1) {
                        layer.msg("批量删除成功");
                    } else {
                        layer.msg("批量删除失败");
                    }
                    tableIns.reload();
                    layer.close(index);
                })
            })
        } else {
            layer.msg("请选择需要删除的文章");
        }
    });

    $(".addNews_btn").click(function () {
        addNews(0);
    });

    //添加文章
    function addNews(id) {
        var index = layui.layer.open({
            title: "添加文章",
            type: 2,
            area: ['', '800px'],
            content: $("#baseURL").val() + "news/newsAdd?id="+id,
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    }

    //列表操作
    table.on('tool(newsList)', function (obj) {
        var layEvent = obj.event;
        var data = obj.data;

        if (layEvent === 'edit') { //编辑
            addNews(data.id);
        } else if (layEvent === 'del') { //删除
            var newsId = [];
            newsId.push(data.id);
            layer.confirm('确定删除此文章？', {icon: 3, title: '提示信息'}, function (index) {
                $.post($("#baseURL").val() + 'news/deleteBatch', {
                    ids: newsId
                }, function (data) {
                    if (data.code == 1) {
                        layer.msg("删除成功");
                    } else {
                        layer.msg("删除失败");
                    }
                    tableIns.reload();
                    layer.close(index);
                })
            });
        } else if (layEvent === 'look') { //预览
            layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
        }
    });
});